# 主页面事件绑定
import share
from binding import mapping_bind, template_bind
from util.ComboBoxUtil import ComboBoxUtil
from util.requestUtil import requestUtil


class home_bind:
    def __init__(self):
        self.ui = share.SI.main.ui

    # 单位/分组下拉事件绑定
    def unit_group_comboBox_fun(self):
        self.ui.unit_comboBox.currentTextChanged.connect(lambda: self.unit_group_comboBox())
        self.ui.group_comboBox.currentTextChanged.connect(lambda: self.unit_group_comboBox())

    # 重新渲染模板及映射数据
    def unit_group_comboBox(self):
        mapping_bind.bind().findMapping()
        template_bind.bind().findAll()

    # 单位/分组下拉框事件
    def get_ug_comboBox_fun(self):
        group_value = ComboBoxUtil(self.ui.group_comboBox).getValue()
        unit_value = ComboBoxUtil(self.ui.unit_comboBox).getValue()
        return {"group_value":str(group_value),"unit_value":str(unit_value)}

    # 下拉框值绑定
    def ug_comboBox_value_fun(self):
        group_data = requestUtil("/template/group/findAll","GET").dataMap["data"]
        unit_data = requestUtil("/template/unit/findAll","GET").dataMap["data"]

        try:
            self.ui.group_comboBox.currentTextChanged.disconnect()
            self.ui.unit_comboBox.currentTextChanged.disconnect()
        except:
            pass

        self.ui.group_comboBox.clear()
        self.ui.unit_comboBox.clear()

        ComboBoxUtil(self.ui.group_comboBox).addItems(group_data,"name","id")
        ComboBoxUtil(self.ui.unit_comboBox).addItems(unit_data,"name","id")

        self.ui.unit_comboBox.currentTextChanged.connect(lambda: self.unit_group_comboBox())
        self.ui.group_comboBox.currentTextChanged.connect(lambda: self.unit_group_comboBox())